package com.lankystar.sos.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lankystar.sos.common.Page;
import com.lankystar.sos.dao.IMessageDao;
import com.lankystar.sos.dao.IRoomDao;
import com.lankystar.sos.model.Message;
import com.lankystar.sos.model.Room;
import com.lankystar.sos.model.User;
import com.lankystar.sos.service.IRoomInterface;

@Service
@Transactional
public class RoomInterface implements IRoomInterface{

	@Autowired
	private IRoomDao roomDao;
	@Autowired
	private IMessageDao messageDao;
	
	@Override
	public void addRoomMethod(Room record) {
		roomDao.addRoom(record);
	}

	@Override
	public void deleteRoomMethod(Room record) {
		roomDao.deleteRoom(record);
	}
	
	@Override
	public void saveMessageMethod(Integer userId,Integer sos_id,String message) {
		User user = roomDao.selectUserByuserId(userId);
		Room room = roomDao.selectRoomBysos_id(sos_id);
		Message record = new Message();
		record.setRoom(room);
		record.setUser(user);
		record.setTime(new Date());
		record.setMessage(message);
		
		messageDao.addMessage(record);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> queryMessageBysos_idMethod(Integer sos_id, int pageSize, int pageNow) {
		Page<Message> page = messageDao.selectMessageBysos_id(sos_id, pageSize, pageNow);
		List<Object[]> oblist = (List<Object[]>) page.getDatas();
		Map<String, Object> map = new HashMap<>();
		Object[] ob = new Object[oblist.size()];
		int i =0;
		for (Object[] objects : oblist) {
			Message msg = (Message) objects[0];
			User user = (User) objects[1];
			Room room = (Room) objects[2];
			Map<String, Object> message = new HashMap<>();
			message.put("userId", user.getUserId());
			message.put("userPortrait", user.getUserPortrait());
			message.put("userNickName", user.getUserNickName());
			message.put("message", msg.getMessage());
			message.put("time", msg.getTime().getTime());
			message.put("sos_id", room.getSos_id());
			ob[i] = message;
			i++;
		}
		map.put("mgs", ob);
		map.put("pageCount", page.getPageCount());
		map.put("pageNow", page.getPageNow());
		map.put("pageSize", page.getPageSize());
		map.put("rowCount", page.getRowCount());
		return map;
	}
	/**
	 * 按条件查询，最新、历史消息
	 */
	@Override
	public Map<String, Object> queryMessageByTime(Integer sos_id, int pageSize, int pageNow, String type) {
		Page<Message> page = messageDao.selectMessageByTime(sos_id, pageSize, pageNow, type);
		List<Object[]> oblist = (List<Object[]>) page.getDatas();
		Map<String, Object> map = new HashMap<>();
		Object[] ob = new Object[oblist.size()];
		int i =0;
		for (Object[] objects : oblist) {
			Message msg = (Message) objects[0];
			User user = (User) objects[1];
			Room room = (Room) objects[2];
			Map<String, Object> message = new HashMap<>();
			message.put("userId", user.getUserId());
			message.put("userPortrait", user.getUserPortrait());
			message.put("userNickName", user.getUserNickName());
			message.put("message", msg.getMessage());
			message.put("time", msg.getTime());
			message.put("sos_id", room.getSos_id());
			ob[i] = message;
			i++;
		}
		map.put("mgs", ob);
		map.put("pageCount", page.getPageCount());
		map.put("pageNow", page.getPageNow());
		map.put("pageSize", page.getPageSize());
		map.put("rowCount", page.getRowCount());
		return map;
	}

}
